const app = getApp();
var util = require('../../utils/util.js')
// var time = this.nowTime();
Page({
  /**
   * 页面的初始数据
   */
  //   [底分, 地主123, 炸弹0+, 输赢01, 得分, 得分, 得分]
  // [10, 2, 0, 0, -20, 10, 10]
  data: {
    lastrecord: wx.getStorageSync('last') || {
      player:
        [{
          avatar: 0,
          name: '张三三',
          score: 0
        }, {
          avatar: 0,
          name: '李四四',
          score: 0
        }, {
          avatar: 0,
          name: '王五五',
          score: 0
        }],
      time: util.formatTime(new Date()),
      winner: 0,
      panshu: 0,
      newscore: [0, 0, 0],
      defen: []
    },
    openid: '',
    formId: '',
    userid: 0,
    userName: '',
    offSuan: false,
    offSet: false,
    formData: {
      difen: 10,
      dizhu: 0,
      bomb: 0,
      win: -1
    }
    //   [底分, 地主123, 炸弹0+, 输赢01, 得分, 得分, 得分]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenid();
    console.log('time = ' + this.nowTime());
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("show_time")
    var list1 = wx.getStorageSync('last') || {
      player:
        [{
          avatar: 0,
          name: '张三三',
          score: 0
        }, {
          avatar: 0,
          name: '李四四',
          score: 0
        }, {
          avatar: 0,
          name: '王五五',
          score: 0
        }],
      time: util.formatTime(new Date()),
      winner: 0,
      panshu: 0,
      newscore: [0, 0, 0],
      defen: []
    };
    this.setData({
      lastrecord: list1
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  ///////////////////////////
  // 打开设置(关闭)
  opensetting: function (event) {
    var uid = event.currentTarget.dataset.user
    console.log("获取到的user=" + uid);
    this.setData({
      offSet: !this.data.offSet
    });
    if (uid != null) {
      this.setData({
        userid: uid,
        userName: this.data.lastrecord.player[uid].name,
        imgId: this.data.lastrecord.player[uid].avatar
      });
    }
  },
  userNameInput: function (e) {
    // console.log("name = " + e.detail.value);
    this.setData({
      userName: e.detail.value
    })
  },
  // 设置头像
  change(e) {
    console.log("head = " + e.currentTarget.dataset.id);
    this.setData({
      imgId: e.currentTarget.dataset.id
    })
  },
  // 保存设置
  saveSetting: function (event) {
    var record1 = this.data.lastrecord;
    var p1 = record1.player;
    p1[this.data.userid].name = this.data.userName;
    p1[this.data.userid].avatar = this.data.imgId;
    record1.player = p1;
    wx.setStorageSync('last', record1);
    // 最新newscore[0,0,0]
    this.setData({
      offSet: !this.data.offSet,
      lastrecord: record1
    });
  },
  // 打开算分
  opencalc: function (event) {
    console.log("获取到的formid=" + event.detail.formId);
    this.setData({
      formId: event.detail.formId,
      offSuan: !this.data.offSuan
    });
  },

  onTabdizhu(e) {
    this.setData({
      ['formData.dizhu']: e.currentTarget.dataset.dizhu
    })
    // console.log(e.currentTarget.dataset.dizhu);
    // console.log(this.data.formData);
  },

  onTabbombmin(e) {
    var b = this.data.formData.bomb;
    if (b > 0) {
      this.setData({
        ['formData.bomb']: b - 1
      })
    }
    // console.log(b);
    // console.log(this.data.formData);
  },

  onTabbombadd(e) {
    var b = this.data.formData.bomb;
    if (b < 14) {
      this.setData({
        ['formData.bomb']: b + 1
      })
    }
    // console.log(b);
    // console.log(this.data.formData);
  },

  onTabwin(e) {
    this.setData({
      ['formData.win']: e.currentTarget.dataset.win
    })
    // console.log(e.currentTarget.dataset.win);
    // console.log(this.data.formData);
  },

  // 算分/保存
  calc: function (event) {
    //   [底分, 地主123, 炸弹0+, 输赢01, 得分, 得分, 得分]
    // [10, 2, 0, 0, -20, 10, 10]
    // { difen: 10, dizhu: "3", bomb: 0, win: "0" }
    var zf = this.data.formData.difen
    if (this.data.formData.bomb > 0) {
      for (var i = 0; i < this.data.formData.bomb; i++) {
        zf = zf * 2
      }
    }
    var a = 0, b = 0, c = 0
    var dz = zf / 2
    if (this.data.formData.win == 0) {
      zf = -zf
    } else {
      dz = -dz
    }
    if (this.data.formData.dizhu == 1) {
      a = zf
      b = c = dz
    }
    if (this.data.formData.dizhu == 2) {
      b = zf
      a = c = dz
    }
    if (this.data.formData.dizhu == 3) {
      c = zf
      b = a = dz
    }

    var record1 = this.data.lastrecord;
    var ns = [record1.newscore[0] + a, record1.newscore[1] + b, record1.newscore[2] + c]
    var win = 0;
    var big = ns[0];
    for (var i = 1; i < ns.length; i++) {
      if (ns[i] > big) {
        big = ns[i];
        win = i;
      }
    }
    record1.winner = win;
    // var nps = this.data.panshu + 1;

    var nps = record1.defen.length + 1;
    console.log('ps=' + nps);

    var newdf = record1.defen;
    // { difen: 10, dizhu: "3", bomb: 0, win: "0" }
    var df = {
      'id': nps,
      difen: this.data.formData.difen,
      dizhu: this.data.formData.dizhu,
      bomb: this.data.formData.bomb,
      win: this.data.formData.win,
      time: util.formatTime(new Date()),
      'a': a,
      'b': b,
      'c': c
    };
    newdf.unshift(df);
    record1.defen = newdf;
    record1.newscore = ns;
    record1.panshu = nps;
    wx.setStorageSync('last', record1);
    // 最新newscore[0,0,0]
    this.setData({
      offSuan: !this.data.offSuan,
      // panshu: nps,
      // newscore: ns,
      // defen: newdf,
      lastrecord: record1,
      formData: {
        difen: 10,
        dizhu: 0,
        bomb: 0,
        win: -1
      }

    });
  },

  // 删除
  del: function (event) {
    var record1 = this.data.lastrecord;
    var nps = record1.panshu - 1;
    var newdf = record1.defen;

    console.log(newdf);
    var aa = newdf.shift();

    var ns = [record1.newscore[0] - aa.a, record1.newscore[1] - aa.b, record1.newscore[2] - aa.c];
    var win = 0;
    var big = ns[0];
    for (var i = 1; i < ns.length; i++) {
      if (ns[i] > big) {
        big = ns[i];
        win = i;
      }
    }
    record1.winner = win;
    record1.panshu = nps;
    record1.newscore = ns;
    record1.defen = newdf;
    wx.setStorageSync('last', record1);
    this.setData({
      // panshu: nps,
      // newscore: ns,
      // defen: newdf
      lastrecord: record1
    });
    this.hideModal();
  },
  //格式化时间显示
  nowTime: function () {
    let date = new Date()
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
  },
  // 获取用户openid
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log(res);
        console.log('云函数获取到的openid: ', res.result.openid)
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
      }
    })
  }
  ///////////////////
})